var customTagsInit = jSouper.customTagsInit;
var modulesInit = jSouper.modulesInit;

var _is_ueditor_css_load = false;
customTagsInit["ueditor"] = function(vm) {
	_is_ueditor_css_load || $('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', 'umeditor1_2_2-utf8-php/themes/default/css/umeditor.css'));
	_is_ueditor_css_load = true;
	var ueditorNode = vm.getOneElementByTagName("ueditor");
	var ueNode = vm.getOneElementByTagName("uecontanier");
	$.when(
		$.getScript("umeditor1_2_2-utf8-php/umeditor.js"),
		$.getScript("umeditor1_2_2-utf8-php/umeditor.config.js")
	).done(function() {
		//语言包
		$.getScript("umeditor1_2_2-utf8-php/lang/zh-cn/zh-cn.js", function() {
			var id = ueNode.id = Math.random().toString(36).substr(2);
			var ue;
			var _ue_init_ti;
			var _ue_set_ti;
			jSouper.onElementPropertyChange(ueditorNode, "value", function _init_value(key, value) {
				if (!ue && document.getElementById(id)) {
					window.ue = ue = UM.getEditor(id);

					ue.setOpt("imageCompressEnable", true); //启用压缩
					ue.setOpt("imageCompressBorder", 1200); //多图上传压缩最大宽度
					ue.setWidth(ueditorNode.clientWidth)

					ue.addListener("contentChange", function() {
						var bind_input_key = ueditorNode.getAttribute("input");
						if (bind_input_key) {
							vm.set(bind_input_key, ue.getContent());
						}
						jSouper.dispatchEvent(ueditorNode, "input");
					});
					ue.ready(function() {
						//DOM remove 后，又append，这是UM的isReady任然认为是1，其实iframe需要重载，所以这边又再次出发，value会被正确绑定
						var value = ueditorNode.getAttribute("value");
						typeof value === "string" && (value != ue.getContent()) && ue.setContent(value);
					});

					clearTimeout(_ue_init_ti);
					var args = Array.prototype.slice.call(arguments);
					var self = this;
					_ue_init_ti = setTimeout(function() {
						_init_value.apply(self, args);
					}, 200)
					return
				}
				value && (value != ue.getContent()) && ue.setContent(value)
			}, true);
			jSouper.onElementPropertyChange(ueditorNode, "input", function(key, bind_input_key) {
				bind_input_key && ue && vm.set(bind_input_key, ue.getContent());
			});
		});
	}).fail(function() {
		console.error(arguments);
	});
};



customTagsInit["weibo-share"] = function(vm) {
	var shareNode = vm.getOneElementByTagName("div");
	vm.set("$CPrivate.$Event.shareToWeibo", function() {
		var title = shareNode.getAttribute("title") || "";
		var src = shareNode.getAttribute("src");
		var href = shareNode.getAttribute("href");
		window.open("http://service.weibo.com/share/share.php?pic=" + encodeURIComponent(src) +
			"&title=" + encodeURIComponent(title.replace(/&nbsp;/g, " ").replace(/<br \/>/g, " ")) +
			"&url=" + encodeURIComponent(href),
			"分享至新浪微博",
			"toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no");
	});
};

customTagsInit["href"] = function(vm) {
	var aNode = vm.getOneElementByTagName("href");
	if (!!history.pushState) {
		//使用History Api封装跳转
		vm.set("$CPrivate.$Event.noreload_jump", function(e) {
			jSouper.dispatchEvent(this, "beforejump");
			if (jSouper.$.strToBool(aNode.getAttribute("stop-jump"))) {
				return false;
			}
			var _to_url = aNode.getAttribute("to");
			if (_to_url) {
				Path.jump(_to_url);
			}
			return false;
		});
	}
};